package com.evaluation.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 通用返回结果类
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

    private boolean success;
    private String message;
    private T data;
    private Integer code;

    public Result() {
    }

    public Result(boolean success, String message, T data, Integer code) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.code = code;
    }

    public static <T> Result<T> success() {
        return new Result<>(true, "操作成功", null, 200);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(true, "操作成功", data, 200);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(true, message, data, 200);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(false, message, null, 500);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(false, message, null, code);
    }

    /**
     * 链式设置数据
     */
    @SuppressWarnings("unchecked")
    public Result<T> data(String key, Object value) {
        if (this.data == null) {
            this.data = (T) new java.util.HashMap<String, Object>();
        }
        if (this.data instanceof java.util.Map) {
            ((java.util.Map<String, Object>) this.data).put(key, value);
        }
        return this;
    }

    // Getters and Setters
    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}
